home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9727 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.8 KB

  1. Path: s02.pavilion.co.uk!usenet
  2. From: AJRobb@pavilion.co.uk (Andy J Robb)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: passing arrays and returning structs
  5. Date: Tue, 12 Mar 1996 23:47:05 GMT
  6. Organization: Pavilion Internet plc
  7. Message-ID: <4i52bm$5el@s02.pavilion.co.uk>
  8. References: <4i2128$69d@news2.acs.oakland.edu>
  9. NNTP-Posting-Host: poolc27.pavilion.co.uk
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. jggoslin@vela.acs.oakland.edu (Monument) wrote:
  13.  
  14. >I have been wracking my brains over this one.  I needed to write a
  15. >program to do the transitive closure of a binary relation matrix, no
  16. >problems encountered during that.  However, I tried to streamline the
  17. >code, by putting the transitive closure algorithm in it's own
  18. >function.  When I did this, I tried to pass the initially read in
  19. >matrix into the function as an argument, and return a message buffer
  20. >to the calling function.
  21.  
  22. >I got a mess of errors, included below for your perusal.  If anyone
  23. >has any suggestions on how I could get the pointers and returns fixed,
  24. >I would sincerely appreciate it.  Post or email, since I read both
  25. >regularly.
  26.  
  27. >===client.h===
  28. >#include <stdio.h>
  29. >#include <sys/errno.h>
  30. >#include <sys/ipc.h>
  31. >#include <sys/msg.h>
  32. >#include <sys/types.h>
  33.  
  34. >#define PATH "./server"
  35. >#define PROJ 'B'
  36. >#define PERMS 0666
  37. >#define MAXSZ 200
  38.  
  39. >key_t key;
  40. >int msgqid;
  41.  
  42. >typedef struct my_msgbuf {
  43. >    long mtype;
  44. >    int pid;
  45. >    int size;
  46. >    int data[2*MAXSZ];
  47. >} Message;
  48. >===client.h===
  49.  
  50. >===client.c===
  51. >// header files
  52. >#include "client.h"
  53.  
  54. >// function prototypes
  55.  
  56. Message is NOT a struct - you typedef'd it.
  57.  
  58. >struct Message transitive_closure(int matrix[]);
  59.  
  60. Message transitive_closure(int matrix[]);
  61.  
  62. >// MAINLINE
  63. >int main(int argc, char *argv[])
  64. >{
  65. >[I am going to delete a bunch of code here that is irrelevant to the
  66. >problem, since I know it works.  All the code I delete does is reads
  67. >in the matrix]
  68.  
  69. >[here is the relevant call]
  70. >    // get the transitive closure
  71. >    buffer = transitive_closure(matrix);
  72.  
  73. >    // print out results
  74. >    puts("Transitive Closure:");
  75. >    for (i=0; i<buffer.size; i++)
  76. >    {
  77. >        for (j=0; j<buffer.size; j++)
  78. >            printf(" %d", buffer.data[i*buffer.size+j]);
  79. >        printf("\n");
  80. >    }
  81.  
  82. >    // successful finish
  83. >    return 0;
  84. >}
  85.  
  86. The following definition is different from the above declaration.
  87. It uses a pointer rather than the struct.  Also, Message is still not
  88. a struct.
  89.  
  90. >struct Message* transitive_closure(int matrix[])
  91.  
  92.  
  93. Message transitive_closure(int matrix[])
  94. >{
  95. >    int i, j, size;
  96. >    Message buffer;
  97. >    pid_t pid;
  98.  
  99. >[here do I refer to the subtypes in the correct manner?  "." vs. "->"
  100. >is what I'm talking about]
  101.  
  102. >    buffer.mtype=1L;
  103. >    buffer.pid=pid;
  104. >    buffer.size=size;
  105. >    for (i=0; i<size; i++)
  106. >        for (j=0; j<size; j++)
  107. >            buffer.data[i*size+j]=matrix[i*size+j];
  108.  
  109. >[is the return type correct?]
  110. No, to be consistent with your redefinition:
  111.     return &buffer;    /* bad news guy, it is on the stack!! */
  112. >    return *buffer;
  113.  
  114. Simply:
  115.     return buffer;
  116. >}
  117. >===client.c===
  118.  
  119. >===ERRORS===
  120. >cc -g -c client.c
  121. >/usr/lib/cmplrs/cc/cfe: Error: client.c, line 58: Functions cannot return a non-object type
  122. >     buffer = transitive_closure(matrix);
  123. >     ---------------------------^
  124. >/usr/lib/cmplrs/cc/cfe: Error: client.c, line 58: Reference an expression of void type or an incomplete type.
  125. >     buffer = transitive_closure(matrix);
  126. >     ---------------------------^
  127. >/usr/lib/cmplrs/cc/cfe: Error: client.c, line 73: redeclaration of 'transitive_closure'; previous declaration at line 11 in file 'client.c'
  128. > struct Message* transitive_closure(int matrix[])
  129. > ----------------^
  130. >/usr/lib/cmplrs/cc/cfe: Error: client.c, line 73: Incompatible function return type for this function
  131. > struct Message* transitive_closure(int matrix[])
  132. > ----------------------------------^
  133. >/usr/lib/cmplrs/cc/cfe: Error: client.c, line 109: Dereference a non-pointer
  134. >     return *buffer;
  135. >     --------^
  136. >*** Exit 1
  137. >Stop.
  138. >===ERRORS===
  139.  
  140. > ----------------------------------------------------------------------------
  141. >|          Jeff Goslin - Monument           | "Oh Bentson, you are so        |
  142. >|      jggoslin@vela.acs.oakland.edu        |  mercifully free from the      |
  143. >|                                           |  ravages of intellect."        |
  144. >| http://www.acs.oakland.edu/links/jggoslin |   --Evil, The Time Bandits     |
  145. > ----------------------------------------------------------------------------
  146. >|   how come everyone elses religion is a cult but your cult is a religion   |
  147. > ----------------------------------------------------------------------------
  148.  
  149. -----BEGIN PGP PUBLIC KEY BLOCK-----
  150. Version: 2.6.2i
  151.  
  152. mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
  153. MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
  154. B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
  155. tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
  156. IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
  157. =/wVD
  158. -----END PGP PUBLIC KEY BLOCK-----
  159.  
  160.